Imports CrystalDecisions.CrystalReports.Engine
Imports CrystalDecisions.Shared
Imports System.IO
Public Class CrystalReportHelper
    Implements IDisposable


    Private _CReportDoc As ReportDocument
    Private _expOptions As ExportOptions
    Private _diskFileDestOptions As DiskFileDestinationOptions

    Public Overloads Sub Dispose() Implements IDisposable.Dispose
        Dispose(True)
        GC.SuppressFinalize(Me)
    End Sub

    Protected Overridable Overloads Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            ' Free other state (managed objects).
        End If
        ' Free your own state (unmanaged objects).
        ' Set large fields to null.
    End Sub

    Protected Overrides Sub Finalize()
        ' Simply call Dispose(False).
        Dispose(False)
    End Sub

    Enum ExportType
        EditableRTF
        PortableDocFormat
        CharacterSeparatedValues
        CrystalReport
        Excel
        ExcelRecord
        RichText
        TabSeperatedText
        Text
        WordForWindows
    End Enum

    Public Sub New()
        Initialize()
    End Sub
    Public Sub New(ByVal FileFullname As String)
        Initialize()
        InitialiseCrystalReport(FileFullname)
    End Sub
    Private Sub Initialize()
        _CReportDoc = New ReportDocument
        _expOptions = New ExportOptions
        _diskFileDestOptions = New DiskFileDestinationOptions
    End Sub

    Public Sub InitialiseCrystalReport(ByVal filepath As String)
        _CReportDoc.Load(filepath)
        _CReportDoc.PrintOptions.PaperSize = PaperSize.PaperA4

    End Sub

    Public Function GetReport() As ReportDocument
        Return _CReportDoc
    End Function


    Public Sub ExportTOFile(ByVal filepath As String, ByVal FileType As ExportFormatType)

        _expOptions.ExportFormatType = FileType
        _diskFileDestOptions.DiskFileName = filepath
        _expOptions.ExportDestinationType = ExportDestinationType.DiskFile


    End Sub
    Public Sub ExportTOFileCSV(ByVal filepath As String, ByVal FileType As ExportFormatType)

        _expOptions.ExportFormatType = FileType

        ExportOptions.CreateCharacterSeparatedValuesFormatOptions.ReportSectionsOption = CsvExportSectionsOption.DoNotExport
        _diskFileDestOptions.DiskFileName = filepath
        _expOptions.ExportDestinationType = ExportDestinationType.DiskFile

    End Sub

    Public Sub PrintAllDoc()

        _CReportDoc.PrintToPrinter(1, True, 0, 0)
    End Sub

    Public Sub PrintDoc(ByVal NoOfCopies As Integer, ByVal DefaultCollate As Boolean, _
    ByVal PageFrom As Int32, ByVal PageTo As Int32)

        _CReportDoc.PrintToPrinter(NoOfCopies, DefaultCollate, PageFrom, PageTo)
    End Sub


    Public Sub ExportReport()
        _CReportDoc.Export(_expOptions)

    End Sub

    Public Sub ExportCrystalReport(ByVal exportType As String, ByVal filePath As String)

        If _CReportDoc.IsLoaded = True Then
            Select Case exportType
                Case ExportFormatType.PortableDocFormat.ToString 'Or "PDF"
                    ExportTOFile(filePath, ExportFormatType.PortableDocFormat)
                Case ExportFormatType.CharacterSeparatedValues.ToString ' Or "CSV"
                    ExportTOFileCSV(filePath, ExportFormatType.CharacterSeparatedValues)

                    'Case ExportFormatType.EditableRTF.ToString 'Or "RTF"
                    '    ExportTOFile(filePath, ExportFormatType.EditableRTF)
                    'Case ExportFormatType.CrystalReport.ToString 'Or "RPT"
                    '    ExportTOFile(filePath, ExportFormatType.CrystalReport)
                    'Case ExportFormatType.Excel.ToString 'Or "XLS"
                    '    ExportTOFile(filePath, ExportFormatType.Excel)
                    'Case ExportFormatType.ExcelRecord.ToString 'Or "XLSR"
                    '    ExportTOFile(filePath, ExportFormatType.ExcelRecord)
                    'Case ExportFormatType.HTML32.ToString
                    '    MsgBox("RTF")
                    'Case ExportFormatType.HTML40.ToString
                    '    MsgBox("RTF")
                    'Case ExportFormatType.NoFormat.ToString
                    '    ExportTOFile(filePath, ExportFormatType.NoFormat)
                    'Case ExportFormatType.RichText.ToString
                    '    ExportTOFile(filePath, ExportFormatType.RichText)
                    'Case ExportFormatType.TabSeperatedText.ToString
                    '    ExportTOFile(filePath, ExportFormatType.TabSeperatedText)
                    'Case ExportFormatType.Text.ToString
                    '    ExportTOFile(filePath, ExportFormatType.Text)
                    'Case ExportFormatType.WordForWindows.ToString
                    '    ExportTOFile(filePath, ExportFormatType.WordForWindows)
            End Select
        End If
        _expOptions.ExportDestinationType = ExportDestinationType.DiskFile
        If exportType <> ExportFormatType.HTML32.ToString And exportType <> ExportFormatType.HTML40.ToString Then
            _expOptions.ExportDestinationOptions = _diskFileDestOptions
        End If

        ExportReport()

    End Sub

    Public Sub SetReportDocument(ByVal report As ReportDocument)
        _CReportDoc = report
    End Sub

    Public Sub SetDataSourceConnection(ByVal ServerName As String, ByVal DB As String, _
                    ByVal username As String, ByVal pw As String)

        'Connection String for SubReports
        For i As Integer = 0 To _CReportDoc.DataSourceConnections.Count - 1
            _CReportDoc.DataSourceConnections.Item(i).SetConnection(ServerName, DB, username, pw)
        Next
        _CReportDoc.SetDatabaseLogon(username, pw, ServerName, DB)

    End Sub

    Public Sub SetParameter(ByVal ParamName As String, ByVal ParamValue As Object)
        _CReportDoc.SetParameterValue(ParamName, ParamValue)

    End Sub

    Public Sub SetParameterForSubReport(ByVal ParamName As String, ByVal ParamValue As Object, ByVal subReport As String)
        _CReportDoc.OpenSubreport(subReport)
        _CReportDoc.SetParameterValue(ParamName, ParamValue, subReport)
    End Sub

    Public Sub SetPrinterSettingsDuplexDefaultOption()
        _CReportDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Default
    End Sub

    Public Sub SetPrinterSettingsDuplexHorizontal()
        _CReportDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Horizontal
    End Sub
    Public Sub SetPrinterSettingsDuplexVertical()
        _CReportDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Vertical
    End Sub
    Public Sub SetPrinterSettingsSimpleOption()
        _CReportDoc.PrintOptions.PrinterDuplex = PrinterDuplex.Simplex
    End Sub
    Public Function SetPrinter(ByVal PrinterPath As String) As Boolean
        Dim result As Boolean = True

        _CReportDoc.PrintOptions.PrinterName = PrinterPath

        Return result
    End Function
    Public Sub Close()
        _CReportDoc.Close()
    End Sub
  
End Class
